/**
 * 
 */
package es.ibm.santillana.mdm.workflow.comun;


import java.io.IOException;
import java.util.Iterator;

import com.ibm.pim.catalog.Catalog;
import com.ibm.pim.catalog.item.Item;
import com.ibm.pim.collaboration.CollaborationItem;
import com.ibm.pim.collaboration.CollaborationStep;
import com.ibm.pim.collaboration.ItemCollaborationArea;
import com.ibm.pim.collection.PIMCollection;
import com.ibm.pim.context.Context;
import com.ibm.pim.context.PIMContextFactory;
import com.ibm.pim.extensionpoints.WorkflowStepFunction;
import com.ibm.pim.extensionpoints.WorkflowStepFunctionArguments;
import com.ibm.pim.utils.Logger;

import es.ibm.santillana.mdm.common.util.SantillanaLogger;
import es.ibm.santillana.mdm.integration.ExportData;
import es.ibm.santillana.mdm.integration.ItemChanges;
import es.ibm.santillana.mdm.integration.ChangesHandler;

/**
 * @author Benito
 *
 */
public class Success implements WorkflowStepFunction {

	/**
	 * 
	 */
	public Success() {
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see com.ibm.pim.extensionpoints.WorkflowStepFunction#in(com.ibm.pim.extensionpoints.WorkflowStepFunctionArguments)
	 */
	@Override
	public void in(WorkflowStepFunctionArguments arg0) {}

	/* (non-Javadoc)
	 * @see com.ibm.pim.extensionpoints.WorkflowStepFunction#out(com.ibm.pim.extensionpoints.WorkflowStepFunctionArguments)
	 */
	@Override
	public void out(WorkflowStepFunctionArguments arg0) {
		System.out.println("OUT del Success Comun");
		Context ctx =  PIMContextFactory.getCurrentContext();
		
		PIMCollection <CollaborationItem> collabItem = arg0.getItems();
		Iterator<CollaborationItem> itCollabItems = collabItem.iterator();
		
		while(itCollabItems.hasNext()) {
			CollaborationItem colItem = itCollabItems.next();
			System.out.println("OUT colItem: " + colItem.getPrimaryKey());
			ChangesHandler changesMod = new ChangesHandler(colItem.getSourceItem(),colItem,colItem.getSourceItem().getCatalog().getName(), colItem.getPrimaryKey(), ctx);
			changesMod.createChanges();
			
			try {
				ExportData exportData = new ExportData();
				exportData.exportXML(changesMod.getIntegrationChanges());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
			
		}
	}

	/* (non-Javadoc)
	 * @see com.ibm.pim.extensionpoints.WorkflowStepFunction#timeout(com.ibm.pim.extensionpoints.WorkflowStepFunctionArguments)
	 */
	@Override
	public void timeout(WorkflowStepFunctionArguments arg0) {
		// TODO Auto-generated method stub

	}

}
